const LRUCache = require("../template")

function testLRUCacheCapacity() {
  console.log(
    "1. 容量为 1 的缓存测试:",
    (() => {
      const cache = new LRUCache(1)
      cache.put(1, "a")
      const get1 = cache.get(1)
      cache.put(2, "b")
      const get1After = cache.get(1)
      const get2 = cache.get(2)
      return get1 === "a" &&
             get1After === -1 &&
             get2 === "b"
    })()
  )

  console.log(
    "2. 容量为 3 的缓存测试:",
    (() => {
      const cache = new LRUCache(3)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(3, "c")
      const get1 = cache.get(1)
      const get2 = cache.get(2)
      const get3 = cache.get(3)
      cache.put(4, "d")
      const get1After = cache.get(1)
      const get2After = cache.get(2)
      const get3After = cache.get(3)
      const get4 = cache.get(4)
      return get1 === "a" &&
             get2 === "b" &&
             get3 === "c" &&
             get1After === -1 &&
             get2After === "b" &&
             get3After === "c" &&
             get4 === "d"
    })()
  )

  console.log(
    "3. 容量为 0 的缓存测试:",
    (() => {
      const cache = new LRUCache(0)
      cache.put(1, "a")
      const get1 = cache.get(1)
      const size = cache.size()
      return get1 === -1 &&
             size === 0
    })()
  )

  console.log(
    "4. 大容量缓存测试:",
    (() => {
      const cache = new LRUCache(10)
      for (let i = 1; i <= 10; i++) {
        cache.put(i, `value${i}`)
      }
      const allPresent = Array.from({ length: 10 }, (_, i) => i + 1).every(i => cache.get(i) === `value${i}`)
      cache.put(11, "value11")
      const firstRemoved = cache.get(1) === -1
      const lastPresent = cache.get(11) === "value11"
      return allPresent && firstRemoved && lastPresent
    })()
  )

  console.log(
    "5. this上下文容量测试:",
    (() => {
      const capacityObj = {
        multiplier: 2,
        createCache: function(capacity) {
          const cache = new LRUCache(capacity * this.multiplier)
          cache.put(1, "test")
          return cache.size()
        }
      }
      return capacityObj.createCache(1) === 1 // 容量 2，添加 1 个元素
    })()
  )

  console.log(
    "6. 复杂对象this绑定容量测试:",
    (() => {
      const capacityConfigObj = {
        config: { baseCapacity: 3 },
        createConfigCache: function() {
          const cache = new LRUCache(this.config.baseCapacity)
          cache.put(1, "a")
          cache.put(2, "b")
          cache.put(3, "c")
          cache.put(4, "d") // 应该移除 1
          return cache.get(1) === -1 && cache.get(4) === "d"
        }
      }
      return capacityConfigObj.createConfigCache()
    })()
  )

  console.log(
    "7. 容量边界测试:",
    (() => {
      const cache = new LRUCache(2)
      cache.put(1, "a")
      cache.put(2, "b")
      cache.put(3, "c") // 应该移除 1
      cache.put(4, "d") // 应该移除 2
      return cache.get(1) === -1 &&
             cache.get(2) === -1 &&
             cache.get(3) === "c" &&
             cache.get(4) === "d" &&
             cache.size() === 2
    })()
  )

  console.log(
    "8. 容量一致性测试:",
    (() => {
      const cache = new LRUCache(5)
      for (let i = 1; i <= 10; i++) {
        cache.put(i, `value${i}`)
      }
      const size = cache.size()
      const lastFive = Array.from({ length: 5 }, (_, i) => i + 6).every(i => cache.get(i) === `value${i}`)
      const firstFive = Array.from({ length: 5 }, (_, i) => i + 1).every(i => cache.get(i) === -1)
      return size === 5 && lastFive && firstFive
    })()
  )
}

testLRUCacheCapacity()
